package com.phonegap.plugins;

import android.content.Context;
import android.database.Cursor;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.pushups.dao.DayOfWeek;
import com.pushups.dao.PushupsProgramDAO;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class ReadDB extends CordovaPlugin {

    /* Actions */
    private static String ACTION_GET_SETS = "getSets";

    private Cursor mCursor = null;

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        if(ACTION_GET_SETS.equals(action)){
            getSetsForWeekDay(args, callbackContext);//callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK,readCurrentCalendarEvent()));
            return true;
        }else{
            return false;
        }
    }

    public void getSetsForWeekDay(JSONArray args, CallbackContext callbackContext){
        int week_num = 0;
        int day_num = 0;
        int category_type = 0;

        try{
            week_num = args.getInt(0);
            day_num = args.getInt(1);
            category_type = args.getInt(2);
        }catch(JSONException ex){

        }
        Context ctx = this.cordova.getActivity().getApplicationContext();
        PushupsProgramDAO dao = new PushupsProgramDAO(ctx);
        ArrayList<DayOfWeek> days = dao.getSetsForDay(ctx, category_type, week_num,day_num);
        Gson gson = new Gson();
        Type listOfTestObject = new TypeToken<ArrayList<DayOfWeek>>(){}.getType();
        String s = gson.toJson(days, listOfTestObject);
        callbackContext.success(s);
    }
}
